package cn.cmcc.online.smsapi.sdk.db.entity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.ParseItemManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.NetUtil;
import cn.cmcc.online.smsapi.sdk.net.NewXyHttpRunnable;
import cn.cmcc.online.smsapi.sdk.net.XyHttpRunnable;
import cn.cmcc.online.smsapi.sdk.queue.SdkTaskQueue;
import cn.cmcc.online.smsapi.sdk.queue.TaskInfo;
import cn.cmcc.online.smsapi.sdk.util.FileUtils;
import cn.cmcc.online.smsapi.sdk.util.KeyManager;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;

/**
 * 系统参数配置表
 * @author Administrator
 *
 */
public class SysParamEntityManager{
	public static final String ID = "id";
	public static final String PKEY = "p_key";
	public static final String PVALUE = "p_value";
	public static final String PEXTENDVALUE = "pextend_value";
	public static final String TABLE_NAME = "tb_sdk_param";
	public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;
	public static final String CREATE_TABLE = "create table  if not exists "  + TABLE_NAME + " ("
			                                                     +ID+" int primary key,"
																 + PKEY+" TEXT,"
																 + PVALUE +" TEXT,"
															 	 + PEXTENDVALUE +" TEXT"						          
													             + ")";

    /* SDK-422 huangzhiqiang 20160524 start */
    /**
     * 根据查询条件获取数据键值对
     * 
     * @param whereClause
     * @param whereArgs
     * @param limit
     * @return
     */
    public static Map<String, String> queryKeyValue(String whereClause, String[] whereArgs, int limit) {
        XyCursor xyCursor = null;
        try {
            xyCursor = DBManager.query(false, TABLE_NAME, new String[] { PKEY, PVALUE }, whereClause, whereArgs, null,
                    null, null, String.valueOf(limit));

            if (xyCursor == null || xyCursor.getCount() == 0) {
                return null;
            }
            Map<String, String> keyValueMap = new HashMap<String, String>();
            while (xyCursor.moveToNext()) {
                keyValueMap.put(xyCursor.getString(xyCursor.getColumnIndex(PKEY)),
                        xyCursor.getString(xyCursor.getColumnIndex(PVALUE)));
            }
            return keyValueMap;
        } catch (Throwable e) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "queryKeyValue error:", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(xyCursor, true);
        }
        return null;
    }

    /**
     * 根据扩展参数获取键值
     * 
     * @param pextend
     * @return
     */
    public static Map<String, String> queryKeyValueByPextend(String pextend) {
        return queryKeyValue(PEXTENDVALUE + "=?", new String[] { pextend }, Integer.MAX_VALUE);
    }

    /**
     * 依据key查询对应的值
     * 
     * @param ctx
     * @param paramKey
     * @return
     */
    public static String queryValueParamKey(Context ctx, String paramKey) {
        Map<String, String> keyValueMap = queryKeyValue(PKEY + "=?", new String[] { paramKey }, 1);
        return (keyValueMap == null || !keyValueMap.containsKey(paramKey)) ? null : keyValueMap.get(paramKey);
    }
    /* SDK-422 huangzhiqiang 20160524 end */

	/**
	 * 新增或更新key的值
	 * 
	 * @param ctx
	 * @param paramKey
	 * @param paramValue
	 * @param pextendValue
	 * @return
	 * @throws Throwable
	 */
	public static long insertOrUpdateKeyValue(Context ctx,String paramKey,String paramValue,String pextendValue) throws Exception{
		/*SDK-666 20161107 zhaojiangwei start */
		if(StringUtils.isNull(paramValue) && StringUtils.isNull(pextendValue)){
			return -1;
		}
		/*SDK-666 20161107 zhaojiangwei end */
	    int result = -1;
		String s=queryValueParamKey(ctx,paramKey);
		ContentValues values = new ContentValues();
		values.put(PKEY, paramKey);
		values.put(PVALUE,paramValue);
		values.put(PEXTENDVALUE, pextendValue);
		if(s != null){
			//更新
		    result = DBManager.update(TABLE_NAME, values, PKEY+"=?",new String[]{paramKey});
		}else{
			//新增
		    result = (int)DBManager.insert(TABLE_NAME, values) ;
		}
		return result;
	}
	public static HashMap<String,Object> cacheMap = new HashMap<String,Object>();
	/**
	 * 依据参数key获取
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static boolean getBooleanParam(Context ctx,String paramkey){
	    return getBooleanParam(ctx,paramkey,false);
	}
	/**
     * 依据参数key获取
     * @param ctx
     * @param key
     * @return
     */
    public static boolean getBooleanParam(Context ctx,String paramkey,boolean defaultValue){
        Object b=cacheMap.get(paramkey);
        boolean res =false;
        try{
            if(b == null){
                String temp=queryValueParamKey(ctx,paramkey);
                if(temp ==null){
                    cacheMap.put(paramkey, defaultValue);
                    res = defaultValue;
                }else if("false".equalsIgnoreCase(temp)){
                    res =false;
                }else{
                    res =true;
                }
                cacheMap.put(paramkey, res);
            }
            else
            {
//                return (Boolean)b;
                return Boolean.parseBoolean(b.toString());
            }
        }catch(Throwable e){
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "getBooleanParam: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
        return res;
    }
	/**
	 * 依据参数key获取
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static int getIntParam(Context ctx,String paramkey){
		Object b=cacheMap.get(paramkey);
		int res=-1;
		try{
			if(b == null){
				String temp=queryValueParamKey(ctx,paramkey);
			    if(temp != null){		    	
			    	cacheMap.put(paramkey, temp);
			    	res = Integer.valueOf(temp); 
			    }
			}
			else
			{
				return Integer.parseInt((String)b);
			}
		}catch(Throwable e){
//			e.printStackTrace();
		    /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "getIntParam: ", e);
            /*SDK-550 hewengao 20160719 end*/
		}
		return res;
	}
	/**
	 * 依据参数key获取
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static String getStringParam(Context ctx,String paramkey){
		Object b=cacheMap.get(paramkey);
		String res=null;
		try{
			if(b == null){
				res=queryValueParamKey(ctx,paramkey);
			    if(res != null){
			    	cacheMap.put(paramkey, res);
			    }
			}
			else
			{
				return (String)b;
			}
		}catch(Throwable e){
//			e.printStackTrace();
		    /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "getStringParam: ", e);
            /*SDK-550 hewengao 20160719 end*/
		}
		return res;
	}
	
	public static void clearOldData(String newChannel){
		try {
			String oldChannel= SysParamEntityManager.queryValueParamKey(Constant.getContext(), Constant.CHANNEL);
			if(!StringUtils.isNull(oldChannel)
					&&!StringUtils.isNull(newChannel)&&!oldChannel.equals(newChannel))
			{//当老渠道和新渠道不一样时，清除数据
			    clearOldData(true);
			}
			
		} catch (Throwable e) {
			// TODO: handle Throwable
		}
	}
	
    public static void clearOldData(boolean isNewChannel){
        try {
            if(isNewChannel){
                SceneconfigManager.deleteAll();
                SceneRuleManager.deleteAll();
                ResdownloadManager.deleteAll();
                XmlResdownloadManager.deleteAll();
                MatchCacheManager.deleteAll();
                /*SDK-732 songzhirong 20170314 start*/
                KeyManager.channel = "";
                XyHttpRunnable.appKeySign = "";
                NewXyHttpRunnable.RSA_PRV_KEY = "";
                /*SDK-732 songzhirong 20170314 end*/
            }
            JarSubInfoManager.deleteAll();     
            // 先删除之前的旧jar包
            FileUtils.deleteAllFile(Constant.getPARSE_PATH());
            // 先删除之前的旧dex文件
            /* RM-1652/yangzhi/2017.06.07---start--- */
            FileUtils.deleteAllFile(Constant.getOutDexPath());
            /* RM-1652/yangzhi/2017.06.07---end--- */
            ParseItemManager.deleteAll();
        } catch (Throwable e) {
            // TODO: handle Throwable
        }
    }
    
    public static void deleteOldFile() {
        try {
            String parseFolderPath = Constant.getContext().getFilesDir().getPath() + File.separator + "parse"
                    + File.separator;

            if (FileUtils.isFileExists(parseFolderPath)) {
                // 删除之前的旧jar包
                FileUtils.deleteFile(parseFolderPath, "ParseUtilCasual_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilEC_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilFinanceL_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilFinanceM_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilFinanceS_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilLife_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilMove_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilTelecom_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilTravel_", ".jar", null);
                FileUtils.deleteFile(parseFolderPath, "ParseUtilUnicom_", ".jar", null);
            }

            /* RM-1652/yangzhi/2017.06.07---start--- */
            String outdexFolderPath = Constant.getOutDexPath();
            /* RM-1652/yangzhi/2017.06.07---end--- */

            if (FileUtils.isFileExists(outdexFolderPath)) {
                // 删除之前的旧dex文件
                FileUtils.deleteDex("ParseUtilCasual_", ".dex", null);
                FileUtils.deleteDex("ParseUtilEC_", ".dex", null);
                FileUtils.deleteDex("ParseUtilFinanceL_", ".dex", null);
                FileUtils.deleteDex("ParseUtilFinanceM_", ".dex", null);
                FileUtils.deleteDex("ParseUtilFinanceS_", ".dex", null);
                FileUtils.deleteDex("ParseUtilLife_", ".dex", null);
                FileUtils.deleteDex("ParseUtilMove_", ".dex", null);
                FileUtils.deleteDex("ParseUtilTelecom_", ".dex", null);
                FileUtils.deleteDex("ParseUtilTravel_", ".dex", null);
                FileUtils.deleteDex("ParseUtilUnicom_", ".dex", null);
            }
        } catch (Throwable e) {
            // TODO: handle Throwable
        }
    }
    
	/**
	 * 新增或更新key的值
	 * @param ctx
	 * @param paramKey
	 * @param paramValue
	 * @param pextendValue
	 * @throws Throwable
	 */
	public static void initParams(Context ctx,String channel,String simIccid,boolean preLoadEnable,boolean smsLocateEnable,Map<String,String> extend) throws Exception
	{
		clearOldData(channel);
		//如果已经有值则不需要重置
        String settingEnhance = SysParamEntityManager.queryValueParamKey(ctx, Constant.SMARTSMS_ENHANCE);
        if(settingEnhance == null){
            if(extend != null){
                settingEnhance = extend.get(Constant.SMARTSMS_ENHANCE);//是否支持上报ICCID
            }
            if(settingEnhance == null){
                settingEnhance ="true";//默认开启
            }
            SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.SMARTSMS_ENHANCE,settingEnhance,null);
            cacheMap.put(Constant.SMARTSMS_ENHANCE, settingEnhance);   
        }
		//如果已经有值则不需要重置
		String netWorkType = SysParamEntityManager.queryValueParamKey(ctx, Constant.SUPPORT_NETWORK_TYPE);
		if(netWorkType == null){
		    if(extend != null){
		        netWorkType = extend.get(Constant.SUPPORT_NETWORK_TYPE);//支持在线升级SDK
		    }
	        if(netWorkType== null){
	            netWorkType="1";//默认wifi
	        }
	        SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.SUPPORT_NETWORK_TYPE,netWorkType,null);
	        cacheMap.put(Constant.SUPPORT_NETWORK_TYPE, netWorkType);   
		}
		//如果已经有值则不需要重置
        
		String updSdkPeriod = SysParamEntityManager.queryValueParamKey(ctx, Constant.ONLINE_UPDATE_SDK_PERIOD);
	    if(updSdkPeriod == null){
	        if(extend != null){
	            updSdkPeriod=extend.get(Constant.ONLINE_UPDATE_SDK_PERIOD);//在线升级SDK的周期,以天为单位
	        }
	        if(updSdkPeriod == null){
	            updSdkPeriod="1";//默认1天
	        }
	        SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.ONLINE_UPDATE_SDK_PERIOD,updSdkPeriod,null);
            cacheMap.put(Constant.ONLINE_UPDATE_SDK_PERIOD, updSdkPeriod);
	    }
		SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.PRELOADENABLE, preLoadEnable+"",null);
		SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.SMSLOCATEENABLE, smsLocateEnable+"",null);
		SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CHANNEL, channel+"",null);
		
		cacheMap.put(Constant.PRELOADENABLE, preLoadEnable);
		cacheMap.put(Constant.SMSLOCATEENABLE, smsLocateEnable);
        cacheMap.put(Constant.CHANNEL, channel);
		String smsLocate1=null;
		String customLocationName=null;
		String customPublicServerUrl = null;
		String CUSTOM_PUBINFO_SERVER_URL = null;
		String CUSTOM_SDK_SERVER_URL = null;
		String onLineUpdateSdk=null;
		String onLineQueryNQSql=null;
		
		String recognizeLevel=null;
		String openPopupDrag = null;
		String popupBgType = null;
		String autoUpdateData = null;
		String SUPPORT_NETWORK_TYPE_MAJOR = null;
		String ONLINE_UPDATE_RES_PERIOD = null;
		
		String sceneCensusOnline = null;  
		String CUSTOM_SDK_RES_DOWNLAOD_URL = null;
		
		String reparseBubbleCycle = null; // 气泡识别失败重新识别短信周期，MatchCacheManager.handObjData使用
		String comparePubNumOperator = null; // 菜单数据是否比较公众号运营商，测试省份数据时可关闭对比，方便获取省份数据
		
		if(extend != null){
			String iccid=extend.get(Constant.SIM_ICCID+"_2");//双卡双带
			String smsLocate2=extend.get(Constant.SMS_LOCATE+"_2");//双卡双带
			if(iccid != null){
				IccidInfoManager.insertIccid(iccid,false,smsLocate2,"","","",ctx);
				 //加入处理队列
				 SdkTaskQueue.addTask(new TaskInfo(1, "simIccid",iccid,"smsLocate",smsLocate2));
			}
			customLocationName=extend.get(Constant.CUSTOM_LOCATION_SERVER_URL);//自定义域名			    
		    customPublicServerUrl=extend.get(Constant.CUSTOM_PUBLIC_SERVER_URL);//自定义域名
		    CUSTOM_PUBINFO_SERVER_URL=extend.get(Constant.CUSTOM_PUBINFO_SERVER_URL);//自定义域名
		    CUSTOM_SDK_SERVER_URL=extend.get(Constant.CUSTOM_SDK_SERVER_URL);//自定义域名
		    onLineUpdateSdk=extend.get(Constant.ONLINE_UPDATE_SDK);//支持在线升级SDK
		    onLineQueryNQSql=extend.get(Constant.QUERY_ONLINE);//是否支持在线查询
		    
		    smsLocate1=extend.get(Constant.SMS_LOCATE);//设置归属地
		    recognizeLevel=extend.get(Constant.RECOGNIZE_LEVEL);
		    String skey=extend.get(Constant.SECRETKEY);//设置私钥
		    openPopupDrag=extend.get(Constant.OPEN_POPUP_DRAG);
		    autoUpdateData = extend.get(Constant.AUTO_UPDATE_DATA);
		    cacheMap.put(Constant.SECRETKEY, skey);	
		    popupBgType = extend.get(Constant.POPUP_BG_TYPE);
		    sceneCensusOnline = extend.get(Constant.SCENE_CENSUS_ONLINE);
		    CUSTOM_SDK_RES_DOWNLAOD_URL= extend.get(Constant.CUSTOM_SDK_RES_DOWNLAOD_URL);
		    SUPPORT_NETWORK_TYPE_MAJOR= extend.get(Constant.SUPPORT_NETWORK_TYPE_MAJOR);  
		    ONLINE_UPDATE_RES_PERIOD= extend.get(Constant.ONLINE_UPDATE_RES_PERIOD);   
            reparseBubbleCycle = extend.get(Constant.REPARSE_BUBBLE_CYCLE);
            comparePubNumOperator = extend.get(Constant.COMPARE_PUBNUM_OPERATOR);
            
            String rsakey=extend.get(Constant.RSAPRVKEY);//设置RSA私钥
            cacheMap.put(Constant.RSAPRVKEY, rsakey);	
		}
		if(customLocationName== null){
			customLocationName="";//使用sdk默认
		}
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_LOCATION_SERVER_URL,customLocationName,null);
	    cacheMap.put(Constant.CUSTOM_LOCATION_SERVER_URL, customLocationName);
	    
	    if(customPublicServerUrl== null){
			customPublicServerUrl="";//使用sdk默认
		}
	    if(!StringUtils.isNull(customPublicServerUrl))
	    {//用第三方的地址
	    	if(NetUtil.isUseHttps())
	    	{
	    		NetUtil.PUBINFO_SERVER_URL_HTTPS=customPublicServerUrl;
	    	}
	    	else
	    	{
		    	NetUtil.serverUrl2=customPublicServerUrl;	    	
	    	}
	    }
	    if(CUSTOM_PUBINFO_SERVER_URL==null){
	    	CUSTOM_PUBINFO_SERVER_URL="";
	    }
        if (!StringUtils.isNull(CUSTOM_PUBINFO_SERVER_URL)) {// 用第三方的地址
            /* KINGMAIL-46 huangzhiqiang 20170417 start */
            if (NetUtil.isUseHttps()) {
                NetUtil.PUBLIC_INFO_SERVER_URL_HTTPS = CUSTOM_PUBINFO_SERVER_URL;
            } else {
                NetUtil.publicInfoServerUrl = CUSTOM_PUBINFO_SERVER_URL;
            }
            /* KINGMAIL-46 huangzhiqiang 20170417 end */
        }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_PUBINFO_SERVER_URL,CUSTOM_PUBINFO_SERVER_URL,null);
	    cacheMap.put(Constant.CUSTOM_PUBINFO_SERVER_URL, CUSTOM_PUBINFO_SERVER_URL);
	    
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_PUBLIC_SERVER_URL,customPublicServerUrl,null);
	    cacheMap.put(Constant.CUSTOM_PUBLIC_SERVER_URL, customPublicServerUrl);
	    
	    if(CUSTOM_SDK_SERVER_URL== null){
			CUSTOM_SDK_SERVER_URL="";//使用sdk默认
		}
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_SDK_SERVER_URL,CUSTOM_SDK_SERVER_URL,null);
	    cacheMap.put(Constant.CUSTOM_SDK_SERVER_URL, CUSTOM_SDK_SERVER_URL);
	    if(!StringUtils.isNull(CUSTOM_SDK_SERVER_URL))
	    {//用第三方的地址
	    	if(NetUtil.isUseHttps())
	    	{
	    		NetUtil.POPUP_SERVER_URL_HTTPS=CUSTOM_SDK_SERVER_URL;
	    	}
	    	else
	    	{
		    	NetUtil.serverUrl=CUSTOM_SDK_SERVER_URL;
	    	}
	    }
	    
	    if(CUSTOM_SDK_RES_DOWNLAOD_URL==null)
	    {
	    	CUSTOM_SDK_RES_DOWNLAOD_URL = "";//使用sdk默认
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_SDK_RES_DOWNLAOD_URL,CUSTOM_SDK_RES_DOWNLAOD_URL,null);
	    cacheMap.put(Constant.CUSTOM_SDK_RES_DOWNLAOD_URL, CUSTOM_SDK_RES_DOWNLAOD_URL);
	    if(!StringUtils.isNull(CUSTOM_SDK_RES_DOWNLAOD_URL))
	    {//用第三方的地址
	    	NetUtil.prex=CUSTOM_SDK_RES_DOWNLAOD_URL;
	    }
	    
	    
		if(onLineUpdateSdk== null){
			onLineUpdateSdk="1";//默认是支持
		}
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.ONLINE_UPDATE_SDK,onLineUpdateSdk,null);
	    cacheMap.put(Constant.ONLINE_UPDATE_SDK, onLineUpdateSdk);
	    
	    if(onLineQueryNQSql==null){
	    	onLineQueryNQSql="1";//默认支持在线查询
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.QUERY_ONLINE,onLineQueryNQSql,null);
	    cacheMap.put(Constant.QUERY_ONLINE, onLineQueryNQSql);
	    
	    if(sceneCensusOnline== null){
	    	sceneCensusOnline="0";//默认不上报
		}
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.SCENE_CENSUS_ONLINE,sceneCensusOnline,null);
	    cacheMap.put(Constant.SCENE_CENSUS_ONLINE, sceneCensusOnline);
	    

	    
	    if(recognizeLevel == null){
	    	recognizeLevel="3";
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.RECOGNIZE_LEVEL,recognizeLevel,null);
	    cacheMap.put(Constant.RECOGNIZE_LEVEL, recognizeLevel);
	    
	    if(openPopupDrag==null)
	    {
	    	openPopupDrag="0";
	    }
	  
	    cacheMap.put(Constant.OPEN_POPUP_DRAG, openPopupDrag);
	    if(autoUpdateData==null)
	    {
	    	autoUpdateData="0";
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.AUTO_UPDATE_DATA,autoUpdateData,null);
	    cacheMap.put(Constant.AUTO_UPDATE_DATA, autoUpdateData);
	    if(popupBgType==null)
	    {
	    	popupBgType = "1";
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.POPUP_BG_TYPE,popupBgType,null);
	    cacheMap.put(Constant.POPUP_BG_TYPE, popupBgType);
	    if(SUPPORT_NETWORK_TYPE_MAJOR==null)
		{
			SUPPORT_NETWORK_TYPE_MAJOR = "2";
		}
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.SUPPORT_NETWORK_TYPE_MAJOR,SUPPORT_NETWORK_TYPE_MAJOR,null);
	    cacheMap.put(Constant.SUPPORT_NETWORK_TYPE_MAJOR, SUPPORT_NETWORK_TYPE_MAJOR);
		
	    if(ONLINE_UPDATE_RES_PERIOD==null)
	    {
	    	ONLINE_UPDATE_RES_PERIOD = "2";
	    }
	    SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.ONLINE_UPDATE_RES_PERIOD,ONLINE_UPDATE_RES_PERIOD,null);
	    cacheMap.put(Constant.ONLINE_UPDATE_RES_PERIOD, ONLINE_UPDATE_RES_PERIOD);
	     
        if (reparseBubbleCycle == null) {
            reparseBubbleCycle = "-1";
        }
        SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.REPARSE_BUBBLE_CYCLE, reparseBubbleCycle, null);
        cacheMap.put(Constant.REPARSE_BUBBLE_CYCLE, reparseBubbleCycle);

        if (comparePubNumOperator == null) {
            comparePubNumOperator = "true";
        }
        SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.COMPARE_PUBNUM_OPERATOR, comparePubNumOperator,
                null);
        cacheMap.put(Constant.COMPARE_PUBNUM_OPERATOR, comparePubNumOperator);
	    
		if(!StringUtils.isNull(simIccid)){
			 IccidInfoManager.insertIccid(simIccid,true,smsLocate1,"","","",ctx);
			 //加入处理队列
			 SdkTaskQueue.addTask(new TaskInfo(1, "simIccid",simIccid,"smsLocate",smsLocate1));
		}
        
        SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.APPVERSION, NetUtil.APPVERSION,null);
        /*SDK-564 hewengao 20160726 start*/
        if(extend != null){
            String customDownUrl = extend.get(Constant.CUSTOM_LOGO_DOWN_URL);// 自定义
            if (!StringUtils.isNull(customDownUrl)) {
                NetUtil.BIZPORT_DOWN_URL = customDownUrl;
            }
        }
        /*SDK-564 hewengao 20160726 end*/
        /*SDK-715 songzhirong 20170105 start*/
        if(extend != null){
            String CONFIG_NOTIFY_TIMEMS = extend.get(Constant.CONFIG_NOTIFY_TIMEMS);// 自定义
            if (StringUtils.isNull(CONFIG_NOTIFY_TIMEMS)) {
            	CONFIG_NOTIFY_TIMEMS = String.valueOf(Constant.addVersionChangeTime);
            }
            SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_NOTIFY_TIMEMS, CONFIG_NOTIFY_TIMEMS,
                    null);
            cacheMap.put(Constant.CONFIG_NOTIFY_TIMEMS, CONFIG_NOTIFY_TIMEMS);
        }
        /*SDK-715 songzhirong 20170105 end*/
        /*SDK-725 songzhirong 20170215 start */
        if(extend != null){
            String CONFIG_UPDATE_CYC = extend.get(Constant.CONFIG_UPDATE_CYC);
            if (StringUtils.isNull(CONFIG_UPDATE_CYC)) {
            	CONFIG_UPDATE_CYC = String.valueOf(720 * Constant.MINUTE);
            }
            SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_UPDATE_CYC, CONFIG_UPDATE_CYC,
                    null);
            cacheMap.put(Constant.CONFIG_UPDATE_CYC, CONFIG_UPDATE_CYC);
        }
        /*SDK-725 songzhirong 20170215 end */
        /*SDK-728 songzhirong 20170302 start */
        if(extend != null){
            String CONFIG_NOTIFY_FIRST_TIME = extend.get(Constant.CONFIG_NOTIFY_FIRST_TIME);
            if (!StringUtils.isNull(CONFIG_NOTIFY_FIRST_TIME)) {
            	 SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_NOTIFY_FIRST_TIME, CONFIG_NOTIFY_FIRST_TIME,
                         null);
                 cacheMap.put(Constant.CONFIG_NOTIFY_FIRST_TIME, CONFIG_NOTIFY_FIRST_TIME);
            }
           
        }
        /*SDK-728 songzhirong 20170302 end */
      
        if(extend != null){
            String CONFIG_WIFI_CONNET_DOWNLOAD_TIME = extend.get(Constant.CONFIG_WIFI_CONNET_DOWNLOAD_TIME);
            if (!StringUtils.isNull(CONFIG_WIFI_CONNET_DOWNLOAD_TIME)) {
            	 SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_WIFI_CONNET_DOWNLOAD_TIME, CONFIG_WIFI_CONNET_DOWNLOAD_TIME,
                         null);
                 cacheMap.put(Constant.CONFIG_WIFI_CONNET_DOWNLOAD_TIME, CONFIG_WIFI_CONNET_DOWNLOAD_TIME);
            }
           
        }
        
        if(extend != null){
            String CONFIG_MONITOR_WIFI = extend.get(Constant.CONFIG_MONITOR_WIFI);
            if (!StringUtils.isNull(CONFIG_MONITOR_WIFI)) {
            	 SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_MONITOR_WIFI, CONFIG_MONITOR_WIFI,
                         null);
                 cacheMap.put(Constant.CONFIG_MONITOR_WIFI, CONFIG_MONITOR_WIFI);
            }
            /* RM-1652/yangzhi/2017.06.07---start--- */
            String CUSTOM_FILES_DIR_PATH = extend.get(Constant.CUSTOM_FILES_DIR_PATH);
            if (!StringUtils.isNull(CUSTOM_FILES_DIR_PATH)) {
                 SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CUSTOM_FILES_DIR_PATH, CUSTOM_FILES_DIR_PATH,
                         null);
                 cacheMap.put(Constant.CUSTOM_FILES_DIR_PATH, CUSTOM_FILES_DIR_PATH);
            }
            /* RM-1652/yangzhi/2017.06.07---end--- */
            
            String appkey = extend.get(Constant.CONFIG_APPKEY);
            if (!StringUtils.isNull(appkey)) {
           	 SysParamEntityManager.insertOrUpdateKeyValue(ctx, Constant.CONFIG_APPKEY, appkey,
                        null);
                cacheMap.put(Constant.CONFIG_APPKEY, appkey);
           }
        }
        
        

	}
	
	public static long getLongParam(String paramkey, long defaultValue, Context ctx){
		Object b=cacheMap.get(paramkey);
		Long res=-1L;
		try{
			if(b == null){
				String temp=queryValueParamKey(ctx,paramkey);
			    if(temp != null){		    	
			    	cacheMap.put(paramkey, temp);
			    	res = Long.valueOf(temp); 
			    }
			}
			else
			{
				return Long.parseLong((String)b);
			}
		}catch(Throwable e){
//			e.printStackTrace();
		    /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "getIntParam: ", e);
            /*SDK-550 hewengao 20160719 end*/
		}
		return res;
	}
	
	/* public static long getLongParam(String key, long defaultValue, Context context) {
	        XyCursor cur = null;
	        try {
	            cur = DBManager.query(TABLE_NAME, new String[] {
	            		PVALUE
	            }, PKEY + "=?", new String[] {
	                key
	            });
	            if (cur != null && cur.moveToNext()) {
	                int index = cur.getColumnIndex(PVALUE);
	                String value = cur.getString(index);
	                if (!StringUtils.isNull(value)) {
	                    return Long.valueOf(value);
	                }
	            } else {
	                if (context != null) {
	                    setParam(key, defaultValue + "");
	                    return defaultValue;
	                }
	            }
	        } catch (Throwable e) {
//	            e.printStackTrace();
	            SDK-550 hewengao 20160719 start
	            LogManager.w(Constant.TAG, "getLongParam: ", e);
	            SDK-550 hewengao 20160719 end
	        } finally {
	            XyCursor.closeCursor(cur, true);
	        }
	        return defaultValue;
	    }*/
	 
	  public static long setParam(String key, String value) {
	        try {
	            ContentValues values = new ContentValues();
	            values.put(PKEY, key);
	            values.put(PVALUE, value);
	          
	            int res = DBManager.update(TABLE_NAME, values, PKEY + "=?", new String[] {
	                key
	            });
	            cacheMap.put(key, value);
	            if (res <= 0) {
	                return DBManager.insert(TABLE_NAME,  values);
	            }
	        } catch (Throwable e) {
//	            e.printStackTrace();
	            /*SDK-550 hewengao 20160719 start*/
	            LogManager.w(Constant.TAG, "setParam: ", e);
	            /*SDK-550 hewengao 20160719 end*/
	        }
	        return 0;
	    }
}
